package com.yunkaweilai.android.activity.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.MyDialogInterface;
import com.yunkaweilai.android.model.login.ShiftModel;
import com.yunkaweilai.android.model.operation.EmployeeListBean;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.MyDialog;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 交班界面
 * Created by Administrator on 2017/7/25.
 */

public class ShiftActivity extends BaseActivity implements MyDialogInterface {

    @BindView(R.id.id_tv_turnover)
    TextView idTvTurnover;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_tv_xj)
    TextView idTvXj;
    @BindView(R.id.id_tv_bank)
    TextView idTvBank;
    @BindView(R.id.id_tv_wx)
    TextView idTvWx;
    @BindView(R.id.id_tv_zfb)
    TextView idTvZfb;
    @BindView(R.id.id_btn_shift)
    Button idBtnShift;
    @BindView(R.id.id_edt_remark)
    EditText idEdtRemark;
    @BindView(R.id.id_llayout_shift)
    LinearLayout idLlayoutShift;
    @BindView(R.id.id_tv_shift_name)
    TextView idTvShiftName;
    @BindView(R.id.id_tv_tk)
    TextView idTvTk;
    @BindView(R.id.id_tv_gd)
    TextView idTvGd;
    @BindView(R.id.id_tv_cz)
    TextView idTvCz;
    private ShiftModel model;
    private String change_employee_id = "0";
    private ArrayList<EmployeeListBean> list = new ArrayList<>();

    @SuppressLint("HandlerLeak")
    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Utils.cleanBaseApplication();
            showToast("交班成功");
            dismissProgressDialog();
            intent2Activity(LoginActivity.class);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shift);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("交班")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        model = BaseApplication.getShiftModel();
        initView();
        initClick();
        list.addAll(model.getData().getInfo().getEmployee_list());
    }

    private void initClick() {
        idBtnShift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialog myDialog = new MyDialog(mAc,ShiftActivity.this,"提示","是否确认交班？");
                myDialog.show();

            }
        });

        idLlayoutShift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShiftManListActivity.actionStart(mAc, list);
            }
        });
    }

    private void isRight() {
        showProgressDialog("交班成功，稍后退出界面!");
        initHttp();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 701://交班人
                    String name = data.getStringExtra(ShiftManListActivity.SHIFT_NAME);
                    idTvShiftName.setText(name);
                    change_employee_id = data.getStringExtra(ShiftManListActivity.SHIFT_ID);
                    break;
            }
        }
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.SHIFT_SAVE)
                .addParams("change_employee_id", change_employee_id)
                .addParams("remark", idEdtRemark.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            mhandler.sendMessageDelayed(new Message(), 3000);
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(R.string.http_error);
                    }
                });
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        idTvBank.setText(model.getData().getInfo().getPrice_list().getBank().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getBank().getOrder_num() + "笔");
        idTvTk.setText(model.getData().getInfo().getPrice_list().getRefund().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getRefund().getOrder_num() + "笔");
        idTvGd.setText(model.getData().getInfo().getPrice_list().getGuadan().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getGuadan().getOrder_num() + "笔");
        idTvCz.setText(model.getData().getInfo().getPrice_list().getMemberBalance().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getMemberBalance().getOrder_num() + "笔");
        idTvXj.setText(model.getData().getInfo().getPrice_list().getCash().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getCash().getOrder_num() + "笔");
        idTvWx.setText(model.getData().getInfo().getPrice_list().getWechat().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getWechat().getOrder_num() + "笔");
        idTvZfb.setText(model.getData().getInfo().getPrice_list().getAlipay().getOrder_price() + " 元 / "
                + model.getData().getInfo().getPrice_list().getAlipay().getOrder_num() + "笔");
        idTvTurnover.setText(model.getData().getInfo().getStore_turnover_price() + " 元 / " +
                model.getData().getInfo().getTotle_order_num() + "笔");
        idTvTime.setText(model.getData().getInfo().getStart_time() + "至" +
                Utils.convDate2Str(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss")
        );
//        model.getData().getInfo().getEnd_time()
    }

    @Override
    public void RightClick() {
        isRight();
    }

    @Override
    public void CancelClick() {

    }
}
